/*
The code examples copyrighted by Nokia Corporation that are included to
this material are licensed to you under following MIT-style License:

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#pragma once


#include <DcpAppletIf>
#include <DcpBrief>
#include <DcpWidget>

#include <QObject>
#include <QString>
#include <QList>

#include <MList>
#include <MContainer>

class DcpWidget;
class MAction;



/*
A singleton class which will hold our settings.
The settings are stored in persistent memory, so, the current
settings are saved between our applet launches.
*/
class SimpleSettingsSingleton : public QObject{
	Q_OBJECT
	
	//static variable holding the pointer to our singleton class object
	static SimpleSettingsSingleton *inst;
	
	//List of possible values for our setting
	QStringList possibleValues;
	
	//index of currently selected value
	unsigned selectedValueIndex;
	
signals:
	//this signal is emmited whenever the value of the setting changes
	void selectionChanged();

public:
	SimpleSettingsSingleton();
	
	~SimpleSettingsSingleton();
	
	//Save current settings to persistent storage (to GConf)
	void Save();
	
	//Load settings from persistent storage (from GConf)
	void Load();
	
	//This function returns reference to our singleton class object
	static SimpleSettingsSingleton& Instance(){
		Q_ASSERT(inst);
		return *inst;
	}
	
	//Returns the list of all possible values for the setting
	QStringList GetListOfPossibleValues(){
		return this->possibleValues;//return a copy of QStringList (copy-on-write)
	}
	
	//Sets the current value for our setting
	void SetValue(QString value);
	
	//Gets current value of our setting
	QString GetCurrentValue()const{
		Q_ASSERT(this->selectedValueIndex < this->possibleValues.size());
		return this->possibleValues[this->selectedValueIndex];
	}
};



/*
This class represents a control panel applet.
*/
class SimpleApplet: public QObject, public DcpAppletIf{
	Q_OBJECT
	Q_INTERFACES(DcpAppletIf)

	//Create settings object (singleton)
	SimpleSettingsSingleton settings;
public:
	
	//override
	virtual void init();
	
	//override
	virtual DcpWidget* constructWidget(int widgetId);

	//override
	virtual QString title() const;
	
	//override
	virtual QVector<MAction *> viewMenuItems();
	
	//override
	virtual DcpBrief* constructBrief(int);

};



/*
This class represents the applet button which appears on the control panel page
where all setting applets are listed. This button displays the applet name
and currently selected value of the setting this applet is responsible of.
*/
class SimpleBrief : public DcpBrief{
	Q_OBJECT
public:
	
	SimpleBrief(){
		//Connect the signal to notify control panel when selected value changes
		//and the Brief button needs to be updated.
		QObject::connect(
				&SimpleSettingsSingleton::Instance(),
				SIGNAL(selectionChanged()),
				this,
				SIGNAL(valuesChanged())
			);
	}
	
	//override
	virtual QString valueText()const{
		return SimpleSettingsSingleton::Instance().GetCurrentValue();
	}
};



/*
This class describes the page which is shown when user selectes the applet from
control panel applets page.
*/
class SimpleAppletWidget : public DcpWidget{
	Q_OBJECT
public:
	SimpleAppletWidget(QGraphicsWidget *parent = 0);
	
private slots:
	void OnItemClicked(const QModelIndex &index);

private:
	
	//override
	bool back();
	
	MList* selectContainer;
	MContainer* container;
};

